//package com.lu.sb210222server.heart;
//
//import io.netty.bootstrap.ServerBootstrap;
//import io.netty.channel.ChannelFuture;
//import io.netty.channel.ChannelOption;
//import io.netty.channel.EventLoopGroup;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.nio.NioServerSocketChannel;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.PostConstruct;
//import javax.annotation.PreDestroy;
//import java.net.InetSocketAddress;
//
//@Component
//public class HeartBeatServer {
//
//    private final static Logger LOGGER = LoggerFactory.getLogger(HeartBeatServer.class);
//    private EventLoopGroup boss = new NioEventLoopGroup(); //(1)
//    private EventLoopGroup work = new NioEventLoopGroup();
//
//    @Value("${socket.port}")
//    private int nettyPort;
//
//    /**
//     * 启动 Netty
//     *
//     * @return
//     * @throws InterruptedException
//     */
//    @PostConstruct
//    public void start() throws InterruptedException {
//        ServerBootstrap bootstrap = new ServerBootstrap() //(2)
//                .group(boss, work)
//                .channel(NioServerSocketChannel.class)// (3)
//                .localAddress(new InetSocketAddress(nettyPort))
//                //保持长连接
//                .childOption(ChannelOption.SO_KEEPALIVE, true)//(4)
//                .childHandler(new HeartbeatInitializer());// (5)
//        //绑定并开始接受传入的连接。
//        ChannelFuture future = bootstrap.bind().sync();//(6)
//        if (future.isSuccess()) {
//            LOGGER.info("启动 Netty 成功");
//        }
//    }
//
//    /**
//     * 销毁
//     */
//    @PreDestroy
//    public void destroy() {                        //(7)
//        boss.shutdownGracefully().syncUninterruptibly();
//        work.shutdownGracefully().syncUninterruptibly();
//        LOGGER.info("关闭 Netty 成功");
//    }
//}
